﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Character_quests
    {
        string[,] _character_quests;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._character_quests = Query.SelectQuery(this.conn, "character_quests", column);
        }

        public string[,] Charracter_quests
        {
            get { return this._character_quests; }
        }

        public void addQuest(string character, int id, int queststatus)
        {
            string[] columns = { "character", "quest_id", "quest_status" };
            string[] values = { character, id.ToString(), queststatus.ToString() };
            Query.InsertQuery(this.conn, "character_quests", columns, values);
        }

        public string[,] getQuestsFromCharacter(string character, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[,] quests = new string[0, this._character_quests.GetLength(1)];

            for (int i = 0; i < this._character_quests.GetLength(0); i++)
            {
                if (this._character_quests[i, 1] == character)
                {
                    Other.ResizetwoDimensArray(ref quests, this._character_quests.GetLength(1), this._character_quests.GetLength(0) + 1);
                    for (int i2 = 0; i2 < this._character_quests.GetLength(1); i2++)
                    {
                        quests[quests.GetLength(0) - 1, i2] = this._character_quests[i, i2];
                    }
                }
            }

            return quests;
        }

        public void deleteQuests(int id)
        {
            Query.DeleteQuery(this.conn, "character_quests", "id=" + id);
        }

        public int getQuestStatus(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();
            int status;

            Int32.TryParse(this._character_quests[id - 1, 3], out status);

            return status;
        }

        public void setStatus(int id, int status)
        {
            Query.UpdateQuery(this.conn, "character_quests", "status", status.ToString(), "id=" + id);
        }


    }
}
